/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameLobby.java
 *
 * Created on Oct 29, 2010, 1:29:04 PM
 */
package com.client;

import NinetyNine.Card;
import NinetyNine.Deck;
import NinetyNine.NinetyNineMessage;
import com.server.NinetyNineGameBeanRemote;

import java.awt.TextField;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import java.util.ArrayList;

import javax.jms.*;
import javax.jms.Connection;
import javax.jms.Session;

/**
 *
 * @author Emma
 */
public class GameLobby extends javax.swing.JFrame implements MessageListener {

    static NinetyNineMessage message;
    boolean first = false;
    int cardIndex = 0;
    ArrayList<JLabel> playerLabelList = null;
    ArrayList<JLabel> myCardLabelList = null;
    ArrayList<Card> myCardList = null;
    ImageIcon back = new ImageIcon(this.getClass().getResource(
            "/images/rearVertical.png"));
    ImageIcon back1 = new ImageIcon(this.getClass().getResource(
            "/images/rearHorizon.png"));

    /** Creates new form GameLobby */
    public GameLobby() throws NamingException {

        initComponents();
        initComponets1();

        initGame();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gameLobbyPanel = new javax.swing.JPanel();
        name4 = new javax.swing.JLabel();
        myName = new javax.swing.JLabel();
        totalCardNum = new javax.swing.JLabel();
        name3 = new javax.swing.JLabel();
        cardCenter = new javax.swing.JLabel();
        add = new javax.swing.JButton();
        substract = new javax.swing.JButton();
        winnerBoard = new javax.swing.JLabel();
        startGameButton = new javax.swing.JButton();
        quit = new javax.swing.JButton();
        myCard1 = new javax.swing.JLabel();
        myCard2 = new javax.swing.JLabel();
        myCard3 = new javax.swing.JLabel();
        myCard4 = new javax.swing.JLabel();
        myCard5 = new javax.swing.JLabel();
        loserBoard = new javax.swing.JLabel();
        currentPlayer = new javax.swing.JLabel();
        image1 = new javax.swing.JLabel();
        image2 = new javax.swing.JLabel();
        image3 = new javax.swing.JLabel();
        image4 = new javax.swing.JLabel();
        readyButton = new javax.swing.JButton();
        name1 = new javax.swing.JLabel();
        name2 = new javax.swing.JLabel();
        gameMenuBar = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        leaderBoard = new javax.swing.JMenuItem();
        logoutButton = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();
        help = new javax.swing.JMenu();
        gameRule = new javax.swing.JMenuItem();
        about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ninety-Nine Game Lobby");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        gameLobbyPanel.setBackground(new java.awt.Color(0, 153, 51));
        gameLobbyPanel.setPreferredSize(new java.awt.Dimension(800, 554));

        name4.setText("Player4");

        myName.setText("Mine");

        totalCardNum.setFont(new java.awt.Font("SimSun", 1, 14));
        totalCardNum.setText("Total");

        name3.setText("Player3");

        cardCenter.setText("cardCenter");

        add.setText("Add ");
        add.setEnabled(false);
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        substract.setText("Subtract ");
        substract.setEnabled(false);
        substract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                substractActionPerformed(evt);
            }
        });

        winnerBoard.setBackground(new java.awt.Color(0, 153, 0));
        winnerBoard.setText("winnerBoard");

        startGameButton.setText("Start Game");
        startGameButton.setEnabled(false);
        startGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startGameButtonActionPerformed(evt);
            }
        });

        quit.setText("Quit");
        quit.setEnabled(false);
        quit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                quitMouseClicked(evt);
            }
        });

        myCard1.setText("myCard1");
        myCard1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myCard1MouseClicked(evt);
            }
        });

        myCard2.setText("myCard2");
        myCard2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myCard2MouseClicked(evt);
            }
        });

        myCard3.setText("myCard3");
        myCard3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myCard3MouseClicked(evt);
            }
        });

        myCard4.setText("myCard4");
        myCard4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myCard4MouseClicked(evt);
            }
        });

        myCard5.setText("myCard5");
        myCard5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myCard5MouseClicked(evt);
            }
        });

        loserBoard.setText("loserBoard");

        currentPlayer.setText("currentPlayer");

        image1.setText("image1");

        image2.setText("image2");

        image3.setText("image3");

        image4.setText("image4");

        readyButton.setText("Ready");

        name1.setText("Player1");

        name2.setText("Player2");

        javax.swing.GroupLayout gameLobbyPanelLayout = new javax.swing.GroupLayout(gameLobbyPanel);
        gameLobbyPanel.setLayout(gameLobbyPanelLayout);
        gameLobbyPanelLayout.setHorizontalGroup(
            gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cardCenter)
                                .addComponent(totalCardNum))
                            .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(quit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(readyButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(startGameButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(82, 82, 82)
                                .addComponent(myCard1)
                                .addGap(48, 48, 48)
                                .addComponent(myCard2)
                                .addGap(55, 55, 55)
                                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(myName)
                                    .addComponent(myCard3)))))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(currentPlayer)
                            .addComponent(loserBoard)
                            .addComponent(winnerBoard))
                        .addGap(158, 158, 158)
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(image2)
                            .addComponent(name2)))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(image1)
                            .addComponent(name1))))
                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(myCard4)
                        .addGap(47, 47, 47)
                        .addComponent(myCard5)
                        .addGap(41, 41, 41)
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(add, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(substract, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addGap(249, 249, 249)
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(name4)
                            .addComponent(image4)))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addGap(94, 94, 94)
                        .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(name3)
                            .addComponent(image3))))
                .addContainerGap(113, Short.MAX_VALUE))
        );
        gameLobbyPanelLayout.setVerticalGroup(
            gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gameLobbyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(winnerBoard)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loserBoard)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addComponent(image3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(name3))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addComponent(image2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(name2))
                    .addComponent(currentPlayer))
                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(cardCenter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalCardNum)
                        .addGap(43, 43, 43)
                        .addComponent(myName))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addComponent(name1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(image1))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addComponent(name4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(image4)))
                .addGap(18, 18, 18)
                .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addComponent(add)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(substract))
                    .addGroup(gameLobbyPanelLayout.createSequentialGroup()
                        .addComponent(startGameButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(readyButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(quit))
                    .addGroup(gameLobbyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(myCard1)
                        .addComponent(myCard3)
                        .addComponent(myCard5)
                        .addComponent(myCard2)
                        .addComponent(myCard4)))
                .addContainerGap(169, Short.MAX_VALUE))
        );

        file.setText("File");

        leaderBoard.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        leaderBoard.setText("Leaderboard");
        leaderBoard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leaderBoardActionPerformed(evt);
            }
        });
        file.add(leaderBoard);

        logoutButton.setText("Logout");
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });
        file.add(logoutButton);

        exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        file.add(exit);

        gameMenuBar.add(file);

        help.setText("Help");

        gameRule.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        gameRule.setText("GameRule");
        gameRule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameRuleActionPerformed(evt);
            }
        });
        help.add(gameRule);

        about.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        about.setText("About");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });
        help.add(about);

        gameMenuBar.add(help);

        setJMenuBar(gameMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(gameLobbyPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 914, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(gameLobbyPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void initComponets1() {

        image1.setText("");
        image1.setIcon(back);
        image2.setText("");
        image2.setIcon(back1);
        image3.setText("");
        image3.setIcon(back1);
        image4.setIcon(back);
        image4.setText("");
        cardCenter.setIcon(back1);
        cardCenter.setText("");
        totalCardNum.setText("Total: 0");
        loserBoard.setText("");
        winnerBoard.setText("");
        currentPlayer.setText("Nobody is playing");

        // ArrayList<JLabel> myCardLabelList = null;
        myCardLabelList = new ArrayList<JLabel>(5);
        myCardLabelList.add(myCard1);
        myCardLabelList.add(myCard2);
        myCardLabelList.add(myCard3);
        myCardLabelList.add(myCard4);
        myCardLabelList.add(myCard5);
        for (int i = 0; i <= 4; i++) {
            myCardLabelList.get(i).setText("");
            myCardLabelList.get(i).setIcon(back1);
            myCardLabelList.get(i).setEnabled(false);

        }
    }

    private void initGame() {

        //client click ready button
        readyButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                if (readyButton.isEnabled()) {
                    logoutButton.setEnabled(false);
                    int result = WelcomePage.beanRemote.ready(WelcomePage.uid);
                    System.err.println(result);
                    if (result == 2) {
                        first = true;
                        readyButton.setEnabled(false);
                    } else if (result == 1) // get rid of ready button
                    {
                        readyButton.setEnabled(false);
                    }
                }
            }
            //else show you are waiting
        });

    }

    public ImageIcon getCardFileName(Card card) {

        String fileName = Integer.toString(card.getSuit()) + "-" +
                Integer.toString(card.getValue());
        ImageIcon Icon = new ImageIcon(this.getClass().getResource(
                "/images/" + fileName + ".gif"));
        return Icon;
    }//get file name of Card

    /*
     * Message Listener
     */
    public void onMessage(Message inMsg) {
        ObjectMessage msg = null;

        try {
            if (inMsg instanceof ObjectMessage) {
                msg = (ObjectMessage) inMsg;
                NinetyNineMessage message = (NinetyNineMessage) msg.getObject();
                UpdateUI(message);
            } else {
                System.out.println("Message of wrong type: "
                        + inMsg.getClass().getName());
            }
        } catch (JMSException e) {
            System.out.println("JMSException in onMessage(): "
                    + e.toString());
        } catch (Throwable t) {
            System.out.println("Exception in onMessage():"
                    + t.getMessage());
        }
    }
    /*
     * menuItem
     */

    private void leaderBoardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leaderBoardActionPerformed

        LeaderBoard lb = new LeaderBoard();
        lb.setVisible(true);
    }//GEN-LAST:event_leaderBoardActionPerformed

    private void gameRuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gameRuleActionPerformed

        gameRules rule = new gameRules();
        rule.setVisible(true);

    }//GEN-LAST:event_gameRuleActionPerformed

    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed

        JOptionPane.showMessageDialog(
                null, "Team JOKE----NinetyNine Card Game Version 1.0");

    }//GEN-LAST:event_aboutActionPerformed

    private void quitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_quitMouseClicked
        if(quit.isEnabled()) {
            logoutButton.setEnabled(true);
            quit.setEnabled(false);
            WelcomePage.beanRemote.iQuit();
        }
    }//GEN-LAST:event_quitMouseClicked

    private void addSubtract(boolean adding) {
        if (add.isEnabled() || substract.isEnabled()) {
            add.setEnabled(false);
            substract.setEnabled(false);
            Card response = WelcomePage.beanRemote.playNinetyNine(
                    myCardList.get(cardIndex), adding);

            if (response == null) {
            } else if (myCardList.get(cardIndex).equalTo(response)) {
                myCard1.setEnabled(true);
                myCard2.setEnabled(true);
                myCard3.setEnabled(true);
                myCard4.setEnabled(true);
                myCard5.setEnabled(true);
            } else {
                myCardLabelList.get(cardIndex).setIcon(back1);
                myCardList.remove(cardIndex);
                myCardList.add(cardIndex, response);
                myCardLabelList.get(cardIndex).setIcon(
                        getCardFileName(response));
            }
        }
    }

    private void cardPlayed() {

        if (myCardList.get(cardIndex).getValue() == 12 ||
                myCardList.get(cardIndex).getValue() == 10) {
            add.setEnabled(true);
            substract.setEnabled(true);
        } else {
            Card respondToOtherCard = WelcomePage.beanRemote.playNinetyNine(
                    myCardList.get(cardIndex), true);
            if (respondToOtherCard == null) {
            } else if (myCardList.get(cardIndex).equalTo(respondToOtherCard)) {
                myCard1.setEnabled(true);
                myCard2.setEnabled(true);
                myCard3.setEnabled(true);
                myCard4.setEnabled(true);
                myCard5.setEnabled(true);
            } else {
                myCardList.remove(cardIndex);
                myCardList.add(cardIndex, respondToOtherCard);
                myCardLabelList.get(cardIndex).setIcon(
                        getCardFileName(respondToOtherCard));
            }
        }
    }

    private void disableCards() {
        myCard1.setEnabled(false);
        myCard2.setEnabled(false);
        myCard3.setEnabled(false);
        myCard4.setEnabled(false);
        myCard5.setEnabled(false);
    }

    private void myCard1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myCard1MouseClicked
        if(myCard1.isEnabled()) {
            disableCards();
            cardIndex = 0;
            cardPlayed();
        }
    }//GEN-LAST:event_myCard1MouseClicked

    private void myCard2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myCard2MouseClicked
        if(myCard2.isEnabled()) {
            disableCards();
            cardIndex = 1;
            cardPlayed();
        }
    }//GEN-LAST:event_myCard2MouseClicked

    private void myCard3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myCard3MouseClicked
        if(myCard3.isEnabled()) {
            disableCards();
            cardIndex = 2;
            cardPlayed();
        }
    }//GEN-LAST:event_myCard3MouseClicked

    private void myCard4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myCard4MouseClicked
        if(myCard4.isEnabled()) {
            disableCards();
            cardIndex = 3;
            cardPlayed();
        }
    }//GEN-LAST:event_myCard4MouseClicked

    private void myCard5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myCard5MouseClicked
        if(myCard5.isEnabled()) {
            disableCards();
            cardIndex = 4;
            cardPlayed();
        }
    }//GEN-LAST:event_myCard5MouseClicked

    private void startGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startGameButtonActionPerformed
        if(startGameButton.isEnabled()) {
            startGameButton.setEnabled(false);
            WelcomePage.beanRemote.startGame();
        }
    }//GEN-LAST:event_startGameButtonActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        WelcomePage.beanRemote.destroyPlayer(WelcomePage.uid);
        System.exit(0);
    }//GEN-LAST:event_exitActionPerformed

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed
        if (logoutButton.isEnabled()) {
            WelcomePage.beanRemote.logout(WelcomePage.uid);
            WelcomePage.wp.setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_logoutButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        WelcomePage.beanRemote.destroyPlayer(WelcomePage.uid);
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        addSubtract(true);
    }//GEN-LAST:event_addActionPerformed

    private void substractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_substractActionPerformed
        addSubtract(false);
    }//GEN-LAST:event_substractActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem about;
    private javax.swing.JButton add;
    private javax.swing.JLabel cardCenter;
    private javax.swing.JLabel currentPlayer;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenu file;
    private javax.swing.JPanel gameLobbyPanel;
    private javax.swing.JMenuBar gameMenuBar;
    private javax.swing.JMenuItem gameRule;
    private javax.swing.JMenu help;
    private javax.swing.JLabel image1;
    private javax.swing.JLabel image2;
    private javax.swing.JLabel image3;
    private javax.swing.JLabel image4;
    private javax.swing.JMenuItem leaderBoard;
    private javax.swing.JMenuItem logoutButton;
    private javax.swing.JLabel loserBoard;
    private javax.swing.JLabel myCard1;
    private javax.swing.JLabel myCard2;
    private javax.swing.JLabel myCard3;
    private javax.swing.JLabel myCard4;
    private javax.swing.JLabel myCard5;
    private javax.swing.JLabel myName;
    private javax.swing.JLabel name1;
    private javax.swing.JLabel name2;
    private javax.swing.JLabel name3;
    private javax.swing.JLabel name4;
    private javax.swing.JButton quit;
    private javax.swing.JButton readyButton;
    private javax.swing.JButton startGameButton;
    private javax.swing.JButton substract;
    private javax.swing.JLabel totalCardNum;
    private javax.swing.JLabel winnerBoard;
    // End of variables declaration//GEN-END:variables

    private void UpdateUI(NinetyNineMessage message) {
        System.err.println(message);
        
        if(message.getKicked() == WelcomePage.uid) {
            WelcomePage.wp.setVisible(true);
            this.setVisible(false);
            this.dispose();
        }
        
        if (message.getPlaying()) {
            totalCardNum.setText("Total: "+Integer.toString(
                    message.getTotal()));
            currentPlayer.setText(message.getCurrentPlayer() + " is playing");
            cardCenter.setIcon(getCardFileName(message.getLastCardPlayed()));
        } else {
            totalCardNum.setText("Total: 0");
            currentPlayer.setText("Nobody is playing");
            cardCenter.setIcon(back1);
        }
        
        if (message.getLoser() != null) {
            loserBoard.setText(message.getLoser() + " lost!");
        } else {
            loserBoard.setText("");
        }
        // System.err.println("before start enable");
        if (first && message.getNumActive() > 1 && !message.getPlaying()) {
            startGameButton.setEnabled(true);
        } else {
            startGameButton.setEnabled(false);
        }

        // System.err.println("before show players");
        if(message.getNumActive()!=0) {
            //show the name of players
            int position = message.getActiveUsernames().indexOf(
                    WelcomePage.uname);
            myName.setText(message.getActiveUsernames().get((position) % 5));
            name1.setText(message.getActiveUsernames().get((position+1) % 5));
            name2.setText(message.getActiveUsernames().get((position+2) % 5));
            name3.setText(message.getActiveUsernames().get((position+3) % 5));
            name4.setText(message.getActiveUsernames().get((position+4) % 5));
        } else {
            myName.setText("No Player");
            name1.setText("No Player");
            name2.setText("No Player");
            name3.setText("No Player");
            name4.setText("No Player");
        }
        // System.err.println("before get hand");
        if (message.getPlaying() && message.getTurn() == 0) {
            // System.err.println("in getting hand");
            myCardList = new ArrayList<Card>(5);
            //get five card from the server
            Deck dc = WelcomePage.beanRemote.getHand(WelcomePage.uid);
            //put Card objects into myCardList
            for (int i = 0; i < 5; i++) {
                myCardList.add(dc.draw());
            }
            for (int i = 0; i < 5; i++) {
                myCardLabelList.get(i).setIcon(getCardFileName(
                        myCardList.get(i)));

                //display the five cards user get
                for (int k = 0; k < 5; k++) {
                    myCardLabelList.get(k).setIcon(getCardFileName(
                            myCardList.get(k)));
                    myCardLabelList.get(k).setEnabled(false);

                }
            }
        }
        // System.err.println("before disable");
        if (message.getPlaying() && (WelcomePage.uid == message.getCurrentUID())
                && (!WelcomePage.uname.equals(message.getLoser()))) {

            myCard1.setEnabled(true);
            myCard2.setEnabled(true);
            myCard3.setEnabled(true);
            myCard4.setEnabled(true);
            myCard5.setEnabled(true);
            quit.setEnabled(true);
        } else {
            // System.err.println("in disable");
            myCard1.setEnabled(false);
            myCard2.setEnabled(false);
            myCard3.setEnabled(false);
            myCard4.setEnabled(false);
            myCard5.setEnabled(false);
            quit.setEnabled(false);
        }
        // System.err.println("before winner");
        if (message.getWinner() != null) {
            JOptionPane.showMessageDialog(null, message.getWinner()+" won!");
            logoutButton.setEnabled(true);
            readyButton.setEnabled(true);
            first=false;
        }


        // activeUsernames
    }
}
